package Chat_program_Rasmus;

import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class Server_Return implements Runnable 
{

	// Globals
	
	Socket SOCK;
	private Scanner INPUT;
	private PrintWriter OUT;
	
	// to display time
	private SimpleDateFormat sdf;
	
	String MESSAGE ="";
	
	//-----------------------------------------------------------------------------
	
	public Server_Return (Socket X) 
	{
		this.SOCK = X;	
	}
	
	//-----------------------------------------------------------------------------
	
	public void CheckConnection() throws IOException
	{
		sdf = new SimpleDateFormat("HH:mm:ss");
		
		if (!SOCK.isConnected())
		{
			// if the user has disconnected, we have to remove their socket from the connectionArray
			// we iterate through the array, and search for the socket number and remove it.
			
			for (int i = 1 ; i <= Server.CurrentUsers.size(); i++)
			{
				if( Server.CurrentUsers.get(i).getSock() == SOCK)
				{
					Server.CurrentUsers.remove(i);
					
				}	
			}
		
			for (int i = 1 ; i <= Server.CurrentUsers.size(); i++)
			{
				Socket TEMP_SOCK = (Socket) Server.CurrentUsers.get(i-1).getSock();
				PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream());
				
				// we want to tell the other users, that that this user has disconnected, as well as the administrator on the console.
				
				TEMP_OUT.println(sdf.format(new Date())+" :"+TEMP_SOCK.getLocalAddress().getHostName() + " was disconnected");
				// Console out 
				System.out.println(sdf.format(new Date())+" :"+TEMP_SOCK.getLocalAddress().getHostName() + " was disconnected");
				
				TEMP_OUT.flush();
			}
				
		}
	}
	
	//-------------------------------------------------------------------------------
	
	public void run()
	{
		try
		{
			try
			{
				INPUT = new Scanner(SOCK.getInputStream());
				OUT = new PrintWriter(SOCK.getOutputStream());
				
				while(true)
				{
					// if socket is still connected
					CheckConnection();
					
					// then check for inputs from the users
					if(!INPUT.hasNext())
						return;
					
					MESSAGE = INPUT.nextLine();
					
					if(MESSAGE.contains("CLOSE**")) {
						
						for (Combi user : Server.CurrentUsers) {
							if (user.getSock().equals(SOCK)) {
								Server.CurrentUsers.remove(user);
								break;
							}
						
						}
						Server.updateList();
					}
					
					// if they write a message, echo it out to the console
					System.out.println("Client said: "+MESSAGE);
					
					
					// also echo the message out to the other users 
					for(int i = 1; i<= Server.CurrentUsers.size(); i++)
					{
						Socket TEMP_SOCK = (Socket) Server.CurrentUsers.get(i-1).getSock();
						PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream());
						TEMP_OUT.println(MESSAGE);
						TEMP_OUT.flush();
						System.out.println(sdf.format(new Date())+" : Server has sent a message to: "+ TEMP_SOCK.getLocalAddress().getHostName());					
					}			
				}			
			}
			finally
			{
				SOCK.close();
				System.out.println(sdf.format(new Date())+" : Socket has been closed for connections");
			}
		}
		catch(Exception X)
		{
			System.out.println(sdf.format(new Date())+" : Socket has failed on connection");
			System.out.println(X);
		}	
	}
	
}
